import 'package:flutter/material.dart';
import 'package:flutter_markdown/flutter_markdown.dart';
import 'package:url_launcher/url_launcher.dart' show launchUrl;
import '../constants/app_docs.dart';
import '../constants/app_constants.dart';
import '../main.dart';

class DocumentationViewer extends StatefulWidget {
  const DocumentationViewer({super.key});

  @override
  State<DocumentationViewer> createState() => _DocumentationViewerState();
}

class _DocumentationViewerState extends State<DocumentationViewer> {
  String _currentDoc = AppDocs.userGuide;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('文档中心'),
        actions: [
          PopupMenuButton<String>(
            icon: const Icon(Icons.menu_book),
            onSelected: (String value) {
              setState(() {
                _currentDoc = value;
              });
            },
            itemBuilder: (BuildContext context) => [
              const PopupMenuItem(
                value: AppDocs.userGuide,
                child: Text('使用指南'),
              ),
              const PopupMenuItem(
                value: AppDocs.about,
                child: Text('关于'),
              ),
            ],
          ),
        ],
      ),
      body: Container(
        color: Colors.grey[50],
        child: SingleChildScrollView(
          padding: const EdgeInsets.all(16.0),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Card(
                elevation: 2,
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(12),
                ),
                child: Padding(
                  padding: const EdgeInsets.all(16.0),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      const Row(
                        children: [
                          Icon(Icons.person, color: AppColors.primary),
                          SizedBox(width: 8),
                          Text(
                            '作者信息',
                            style: TextStyle(
                              fontSize: 18,
                              fontWeight: FontWeight.bold,
                            ),
                          ),
                        ],
                      ),
                      const Divider(height: 24),
                      Text('作者：${MyApp.author}'),
                      const SizedBox(height: 4),
                      Text('邮箱：${MyApp.email}'),
                    ],
                  ),
                ),
              ),
              const SizedBox(height: 16),
              Card(
                elevation: 2,
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(12),
                ),
                child: Padding(
                  padding: const EdgeInsets.all(16.0),
                  child: MarkdownBody(
                    data: _currentDoc,
                    selectable: true,
                    styleSheet: MarkdownStyleSheet(
                      h1: const TextStyle(
                        fontSize: 24,
                        fontWeight: FontWeight.bold,
                      ),
                      h2: const TextStyle(
                        fontSize: 20,
                        fontWeight: FontWeight.bold,
                      ),
                      p: const TextStyle(
                        fontSize: 16,
                        height: 1.5,
                      ),
                    ),
                    onTapLink: (text, href, title) {
                      if (href != null) {
                        launchUrl(Uri.parse(href));
                      }
                    },
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
